var EXTENSION_ID = "extension-02";
var SECURITY_LENGTH = 5;
var REMOTE_URL = 'http://achatell.free.fr/';
var STATIC_STORAGE = ['timestamp', 'elementList'];
var DYNAMIC_STORAGE = [{name: 'elementList', types: ['date', 'frequency', 'url', 'postit']}];
var STORAGE_SEPARATOR = '|';
var NAME_MAXLENGTH = 20;


function saveModifiedDate(element){
	storageSet(element + '-date', new Date().getTime());
}

function getModifiedDate(element){
	return storageGet(element + '-date');
}


function getRemainingTime(element){
	var currentTime = new Date().getTime();
	var storedTime = getModifiedDate(element);
	return currentTime - storedTime - storageGet(element + '-frequency') * MILLISECONDS * SECONDS * MINUTES;
}

function createElement(createName, frequency, url) {
	var isNameExist = storageGet('elementList').split('|').some(
		function (element, index, array) {
			return (element == createName);
		}
	);
	if(createName == ""){
		alert("Please enter a name.");
		return false;
	} else if(isNameExist){
		alert("This name already exists, please choose an other one.");
		return false;
	} else if(createName.length > NAME_MAXLENGTH){
		alert("This name is too long, please choose a shorter name.");
		return false;
	} else {
		if(storageGet('elementList') != ""){
			storageSet('elementList', storageGet('elementList') + '|' + createName);	
		} else {
			storageSet('elementList', storageGet('elementList') + createName);	
		}
		if(url){
			storageSet(createName + '-url', url);
		} else {
			storageSet(createName + '-url', "http://www.");
		}
		storageSet(createName + '-date', new Date().getTime());
		storageSet(createName + '-postit', "You can write a memo here");
		storageSet(createName + '-frequency', frequency);
		return true;
	}
}


function getFrequency(name){
	var result;
	var value = getValueInput(name + '-frequency');
	result = getOptionSelect(name + '-frequency', 'select');
	setOptionFrequency(result, value);
	return result;
}



function setOptionFrequency(element, value){
	function getOption(label, valueParam){
		var result = "<option value='" + valueParam + "' ";
		if(value == valueParam){
			result += "selected=selected";
		}
		result +=">" + label + "</option>";
		return result;
	}
	element.append(getOption("60 minutes", 1));
	element.append(getOption("2 hours", 2));
	element.append(getOption("4 hours", 4));
	element.append(getOption("8 hours", 8));
	element.append(getOption("12 hours", 12));
	element.append(getOption("18 hours", 18));
	element.append(getOption("24 hours", 24));
	element.append(getOption("2 days", 48));
	element.append(getOption("4 days", 96));
	element.append(getOption("7 days", 168));
	element.append(getOption("2 weeks", 336));
	element.append(getOption("4 weeks", 672));
	element.append(getOption("2 months", 1344));
	element.append(getOption("6 months", 4320));
	element.append(getOption("12 months", 8640));	
}

function extractNameFromUrl(url){
	result = url.replace("http://", "").replace("https://", "").replace("www.","");	
	if(result.contains("/")){
		result = result.substring(0, result.indexOf("/"));
	}
	if(result.contains(".")){
		result = result.substring(0, result.indexOf("."));
	}
	return getNameWithUppercase(result);
}


function saveUrl(){
	chrome.tabs.getSelected(null,function(tab){
		if(localStorage.currentUrl != tab.url){
			localStorage.savedUrl = localStorage.currentUrl;
			localStorage.currentUrl = tab.url;
		}
	});
}

function notify(id){
		if(storageGet('watchedElement') && storageGet('watchedElement') != ""){
			chrome.pageAction.setIcon({tabId: id, path: "notifierBlue.png"});
			chrome.pageAction.show(id);
		} else {
			var count = 0;
			if(storageGet('elementList') != ""){
				$.each(
					storageGet('elementList').split('|'),
					function(intIndex, element){
						if(getRemainingTime(element) > 0){
							count++;
						}
					}
				);
			}
			if(count > 0){
				chrome.pageAction.setIcon({tabId: id, path: "notifierRed.png"});
				chrome.pageAction.show(id);
			} else {
				chrome.pageAction.setIcon({tabId: id, path: "notifierBlue.png"});
				chrome.pageAction.hide(id);
			}
		}
}      

